﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LingDong.HtmlClassification
{
    public class BayesClassification
    {
        public static void ReadLearnResult()
        {
            LearnResult = new BayesLearn();
            LearnResult.ReadResult();
        }

        public static int ClassificationHtml(string html)
        {
            int[] featureList = BayesUtility.GetHtmlFeature(html);

            double[] categoryProbability = new double[BayesUtility.CategoryNumber];
            for (int i = 0; i < BayesUtility.CategoryNumber; i++)
            {
                categoryProbability[i] = Math.Log(BayesUtility.PriorProbability[i]);
                for (int j = 0; j < BayesUtility.FeatureNumber; j++)
                {
                    categoryProbability[i] += Math.Log(LearnResult.P_f_c[j, i, featureList[j]]);
                }
            }

            int maxIndex = 0;
            for (int i = 0; i < BayesUtility.CategoryNumber; i++)
            {
                if (categoryProbability[i] >= categoryProbability[maxIndex])
                    maxIndex = i;
            }
            return maxIndex;
        }

        public static BayesLearn LearnResult { get; private set; }
    }
}
